import user from '@/lib/request/user'
import { RootState } from '../index'
import { Commit } from 'vuex'
export interface LoginState {
  [key: string]: any
  username: string
  phone: string
  code: string
  licenseNumber: string
  mobileCode: string
  signUpFlag: boolean
  showLogin: boolean
}
const initState: LoginState = {
  username: '',
  phone: '',
  code: '',
  licenseNumber: '',
  mobileCode: '',
  signUpFlag: false,
  showLogin: false,
}

const getters = {}

const actions = {
  login(
    context: {
      commit: Commit
      state: LoginState
      rootState: RootState
      getters: any
    },
    payload: any
  ) {
    return user.login(context.state.phone, context.state.mobileCode)
  },
  logout(
    context: {
      commit: Commit
      state: LoginState
      rootState: RootState
      getters: any
    },
    payload?: any
  ) {
    return user.logout()
  },
  sendMobileCode(
    context: {
      commit: Commit
      state: LoginState
      rootState: RootState
      getters: any
    },
    payload?: any
  ) {
    return user.sendMobileCode(context.state)
  },
  sendLoginCode(
    context: {
      commit: Commit
      state: LoginState
      rootState: RootState
      getters: any
    },
    payload?: any
  ) {
    return user.sendLoginCode(context.state)
  },
  signup(context: {
    commit: Commit
    state: LoginState
    rootState: RootState
    getters: any
  }) {
    return user.signUp(context.state)
  },
}

const mutations = {
  setStateData(state: LoginState, data: any) {
    // eslint-disable-next-line no-param-reassign
    state = Object.assign(state, data)
  },
}

export default {
  namespaced: true,
  state: initState,
  getters,
  actions,
  mutations,
}
